package com.neusoft.bizcore.auth.model;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.validation.constraints.NotBlank;

import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;
import org.hibernate.validator.constraints.Length;

import com.neusoft.bizcore.web.jpa.model.AbstractAuditModel;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Getter
@Setter
@ToString
@Entity
public class AuthUser extends AbstractAuditModel {

    private static final long serialVersionUID = 1L;

    /** 用户名 */
    @NotBlank(message = "用户名不能为空")
    @Length(max = 50, message = "用户名不能超过{max}位")
    @Column(length = 50, nullable = false, unique = true)
    private String username;

    /** 密码 */
    @NotBlank(message = "密码不能为空")
    @Length(max = 255, message = "密码不能超过{max}位")
    @Column(length = 255, nullable = false)
    private String password;

    /** 姓名 */
    @Length(max = 50, message = "姓名不能超过{max}位")
    @Column(length = 50, nullable = true)
    private String name;

    /** 邮件 */
    @Length(max = 50, message = "邮件不能超过{max}位")
    @Column(length = 50, nullable = true)
    private String mail;

    /** 电话 */
    @Length(max = 50, message = "电话不能超过{max}位")
    @Column(length = 50, nullable = true)
    private String phone;

    /** 锁定标志 */
    private boolean locked = false;

    /** 可用标志 */
    private boolean enabled = false;

    /** MD5加密salt */
    private String salt = "";

    /** 维护关系方 */
    @ManyToMany(targetEntity = AuthRole.class)
    @JoinTable(name = "auth_ref_user_roles")
    @LazyCollection(LazyCollectionOption.FALSE)
    private List<AuthRole> roles = new ArrayList<>();

    /**
     * 关联权限
     */
    @ManyToMany(targetEntity = AuthPermission.class)
    @JoinTable(name = "auth_ref_user_permissions")
    @LazyCollection(LazyCollectionOption.FALSE)
    private List<AuthPermission> permissions = new ArrayList<>();

    /**
     * 关联组织
     */
    @ManyToMany
    @JoinTable(name = "auth_ref_org_users")
    @LazyCollection(LazyCollectionOption.FALSE)
    private List<AuthOganization> oganizations = new ArrayList<>();

}
